# coding=utf-8
from . import admin
from flask import render_template, request, jsonify, make_response
from dbConnection.MysqlConn import Mysql
from app import util, PAGE_NUM, page_code
from app.util import login_required
import sys
import time, math

reload(sys)
sys.setdefaultencoding('utf8')


# 用户分享列表
@admin.route('/admin/user_share.html')
@login_required
def user_share():
    return render_template('admin/share/user_share.html')


# 获取用户分享数据
@admin.route('/admin/user_share_post', methods=['post'])
@login_required
def user_share_post():
    parms = ['uid', 'stime', 'etime']
    maps = {}
    for d in parms:
        maps[d] = request.values.get(d)
    p = request.values.get('p')
    c_num = (int(p) - 1) * PAGE_NUM
    sql = "select a.*, b.phone FROM news_location as a LEFT JOIN news_user as b ON b.uid = a.uid WHERE 1=1"
    sql2 = "select count(a.locateId) con FROM news_location as a LEFT JOIN news_user as b ON b.uid = a.uid WHERE 1=1"
    if maps['uid']:
        sql += " and (a.uid = '%s' or locate('%s', b.phone)>0)" % (maps['uid'], maps['uid'])
        sql2 += " and (a.uid = '%s' or locate('%s', b.phone)>0)" % (maps['uid'], maps['uid'])
    if maps['stime']:
        sql += " and a.addTime >= '%s'" % maps['stime']
        sql2 += " and a.addTime >= '%s'" % maps['stime']
    if maps['etime']:
        d_str = maps['etime'] + ' 23:59:59'
        sql += " and a.addTime <= '%s'" % d_str
        sql2 += " and a.addTime <= '%s'" % d_str
    sql += " order by a.addTime desc limit %d, %d" % (c_num, PAGE_NUM)
    mysql = Mysql()
    res = mysql.getAll(sql, None)
    cont = mysql.getOne(sql2, None)
    mysql.dispose()
    pages = int(math.ceil(cont['con'] / float(PAGE_NUM))) if cont else 0
    route_path = 'get_data'
    html_str = page_code.page_code_html_post(int(p), route_path, pages) if pages > 0 else ''
    if res:
        for d in res:
            d['addTime'] = str(d['addTime'])
            d['newTime'] = str(d['newTime']) if d['newTime'] else ''
            if d['locateType'] == 1:
                d['locateType'] = u"新闻"
            elif d['locateType'] == 2:
                d['locateType'] = u"图片"
            elif d['locateType'] == 3:
                d['locateType'] = u"红包"
            elif d['locateType'] == 4:
                d['locateType'] = u"视频"
        data = {
            'status': 1,
            'res': res,
            'page': html_str,
            'p': p
        }
    else:
        data = {
            'status': 2,
            'page': html_str
        }
    return jsonify(data)


# 定位列表
@admin.route('/admin/user_position.html')
@login_required
def user_position():
    return render_template('admin/share/user_position.html')


# 获取定位列表数据
@admin.route('/admin/user_position_post', methods=['post'])
@login_required
def user_position_post():
    parms = ['uid', 'stime', 'etime']
    maps = {}
    for d in parms:
        maps[d] = request.values.get(d)
    p = request.values.get('p')
    c_num = (int(p) - 1) * PAGE_NUM
    sql = "select t.locateType, t.id, t.pointNum, t.locateAddress, t.locateTime, b.phone, a.nickname, a.addTime, " \
          "t.locateTime, a.locateType as ftype FROM news_location_type t LEFT JOIN news_location a ON a.locateId = t.locateId " \
          "LEFT JOIN news_user b ON b.uid = t.uid WHERE 1=1"
    sql2 = "select count(t.id) con FROM news_location_type t LEFT JOIN news_location a ON a.locateId = t.locateId " \
           "LEFT JOIN news_user b ON b.uid = t.uid WHERE 1=1"
    if maps['uid']:
        sql += " and (t.uid = '%s' or locate('%s', b.phone)>0)" % (maps['uid'], maps['uid'])
        sql2 += " and (t.uid = '%s' or locate('%s', b.phone)>0)" % (maps['uid'], maps['uid'])
    if maps['stime']:
        sql += " and t.locateTime >= '%s'" % maps['stime']
        sql2 += " and t.locateTime >= '%s'" % maps['stime']
    if maps['etime']:
        d_str = maps['etime'] + ' 23:59:59'
        sql += " and t.locateTime <= '%s'" % d_str
        sql2 += " and t.locateTime <= '%s'" % d_str
    sql += " order by t.locateTime desc limit %d, %d" % (c_num, PAGE_NUM)
    mysql = Mysql()
    res = mysql.getAll(sql, None)
    cont = mysql.getOne(sql2, None)
    mysql.dispose()
    pages = int(math.ceil(cont['con'] / float(PAGE_NUM))) if cont else 0
    route_path = 'get_data'
    html_str = page_code.page_code_html_post(int(p), route_path, pages) if pages > 0 else ''
    if res:
        for d in res:
            d['addTime'] = str(d['addTime'])
            d['locateTime'] = str(d['locateTime']) if d['locateTime'] else ''
            if d['ftype'] == 1:
                d['ftype'] = u"新闻"
            elif d['ftype'] == 2:
                d['ftype'] = u"图片"
            elif d['ftype'] == 3:
                d['ftype'] = u"红包"
            elif d['ftype'] == 4:
                d['ftype'] = u"视频"
            if d['locateType'] == 0:
                d['locateType'] = u"模糊定位"
            else:
                d['locateType'] = u"精准定位"
        data = {
            'status': 1,
            'res': res,
            'page': html_str,
            'p': p
        }
    else:
        data = {
            'status': 2,
            'page': html_str
        }
    return jsonify(data)
